var vue = new Vue({
    el: "#app",
    data: {
        shopcart: {},
        pageNo: 1,
        pageSize: 5,
        openFlag: false,
        results: [],
        mainFlag: true
    },
    computed: {
        //计算总价
        totalPrices() {
            let total = 0;
            // console.log(total);
            this.results.forEach(function (item, index) {
                //把选中的记录累加
                if (item.checked) {
                    total += item.num * item.price;
                }
            })
            return total;
        },
        checkedSize(){
            let cartLength=0;
            this.results.forEach(item=>{
                if (item.checked) {
                    cartLength++;
                }
            })
            return cartLength;
        }
    },
    mounted() {
        //加载数据
        this.loadData();
    },
    methods: {
        loadData() {
            let params = {pageNo: this.pageNo, pageSize: this.pageSize};
            axios.get("http://localhost:8080/findShopCart", {params}).then(res => {
                let results = res.data.list;
                results.forEach(function (obj) {
                    obj.checked = true;
                    obj.isShow = true;
                })
                this.results = results;
                // console.log(this.results);
            }).catch(function (err) {
                console.log("服务忙，请稍后....");
            })
        },
        //删除
        delItem(deleteId, index) {
            axios.get("delete?id=" + deleteId).then(res => {
                if (res) {
                    this.results[index].isShow = false;
                }
            }).catch(err => {
                alert("删除失败,服务器异常！")
            });
        },
        inputChange(index) {
            this.results[index].num = parseInt(this.results[index].num);
            if (!(this.results[index].num > 0)) {
                console.log(1);
                this.results[index].num = 1;
            }
            if (this.results[index].num >= this.results[index].totalCount) {
                // console.log(this.results[index].num+":"+this.results[index].totalCount);
                this.results[index].num = this.results[index].totalCount;
            }
        },
        //新增数量
        plusItem(index) {
            this.results[index].num++;
            this.inputChange(index);
        },
        //减少数量
        minusItem(index) {
            if (!(this.results[index].num - 1) > 0) {
                this.results[index].num = 1;
            } else {
                this.results[index].num--;
            }
        },
        //打开添加购物车弹框
        openAddDialog() {
            this.openFlag = true;
        },
        //点击遮罩关闭
        hideAddDialog() {
            this.openFlag = false;
        },
        //复选控制
        checkItem(index) {
            this.results[index].checked = !this.results[index].checked;
            // this.$forceUpdate();强制更新数据
            if (!this.results[index].checked) {
                this.mainFlag = false;
            }
            this.selectMainChecked();
        },
        selectMainChecked() {
            let checkLength = 0;//let checkArr=[];
            this.results.forEach(function (item) {
                if (item.checked) {
                    checkLength++;//checkArr.push(1);
                }
            });
            //控制复选框选中，原理是选中元素和数据中条数比较
            this.mainFlag = checkLength == this.results.length;
        },
        //全选
        selectAll() {
            this.mainFlag = !this.mainFlag;
            this.results.forEach((item) => {
                item.checked = this.mainFlag;
                console.log(item+":"+this.mainFlag);
            });
           /*for (let i=0;i<this.results.length;i++){
               this.results[i].checked=this.mainFlag;
            }*/
        },
        //添加购物车
        saveShopCart() {
            axios.post("http://localhost:8080/saveShopCart", this.shopcart).then(res => {
                if (res.status == 200 && res.data > 0) {
                    alert("添加成功!");
                    //重新加载数据
                    this.loadData();
                    //关闭弹出框
                    this.openFlag = false;
                    //清空数据
                    this.shopcart = {};
                }
            }).catch(err => {
                console.log("添加失败....:" + err);
            });
        }
    }
})